﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface;

namespace MaxStudio.Concrete.Server.Service
{
    public class ProductOrderService : ServiceHandle, IProductOrder
    {
        #region IProductOrder Members

        public OperationResult<List<tb_ProductOrder>> GetAllProductOrder()
        {
            OperationResult<List<tb_ProductOrder>> result = new OperationResult<List<tb_ProductOrder>>();
            ConcreteDaoDataContext dataContext = getDateContext();
            List<tb_ProductOrder> list = new List<tb_ProductOrder>();
            try
            {
                list = dataContext.GetAllProductOrder();
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_ProductOrder> GetProductOrderById(int id)
        {
            OperationResult<tb_ProductOrder> result = new OperationResult<tb_ProductOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();
            tb_ProductOrder productOrder;
            try
            {
                productOrder = dataContext.GetProductOrderById(id);
                if (productOrder == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = productOrder;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_ProductOrder> GetProductOrderBySalesOrderId(int salesOrderId)
        {
            OperationResult<tb_ProductOrder> result = new OperationResult<tb_ProductOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();
            tb_ProductOrder productOrder;
            try
            {
                productOrder = dataContext.GetProductOrderBySalesOrderId(salesOrderId);
                if (productOrder == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = productOrder;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_ProductOrder> CreateProductOrder(tb_ProductOrder entity)
        {
            OperationResult<tb_ProductOrder> result = new OperationResult<tb_ProductOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.CreateProductOrder(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_ProductOrder> ModifyProductOrder(tb_ProductOrder entity)
        {
            OperationResult<tb_ProductOrder> result = new OperationResult<tb_ProductOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.ModifyProductOrder(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_ProductOrder> DeleteProductOrder(tb_ProductOrder entity)
        {
            OperationResult<tb_ProductOrder> result = new OperationResult<tb_ProductOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.DeleteProductOrder(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        #endregion
    }
}
